package com.fch.sdlibrary.config;

import lombok.SneakyThrows;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * @program: sdlibrarySpring
 * @description: 配置虚拟路径映射访问
 * @CreatedBy: fch
 * @create: 2022-05-14 10:26
 **/
@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Override
    @SneakyThrows
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**").addResourceLocations("file:" + getPath());
    }

    private String getPath() throws FileNotFoundException {
        File path;
        path = new File(ResourceUtils.getURL("classpath:").getPath());
        String pathStr = path.getAbsolutePath().replace("\\target\\classes", "");
        return pathStr + "\\src\\main\\resources\\static\\img\\";
    }
}
